SdcTreatPreCondition = {
    qCRuleCompare: "yes",
    qCRuleId: "",
};


SdcTreatPreCondition.showPremiseTable = $HUI.datagrid("#showPremiseTable", {
    fit: true,
    pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
    displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
    singleSelect: true,
    checkOnSelect: true,
    //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
    autoRowHeight: false,
    striped: true,
    rownumbers: true,
    loadMsg: '数据加载中...',
    pageSize: 10,
    pageList: [10, 50],
    idField: "id",
    columns: [[
        {field: 'ruleCode', title: '编码', width: '80', align: 'left', sortable: false},
        {field: 'ruleDesc', title: '规则名称', width: '200', align: 'left', sortable: false},
        {field: 'comment', title: '备注', width: '80', align: 'left', sortable: false},
        {field: 'ruleType', title: '规则类型', width: '200', align: 'left', sortable: false},
        {field: 'calType', title: '计算类型', width: '200', align: 'left', sortable: false},
        {field: 'isDefault', title: '默认值', width: '200', align: 'left', sortable: false,formatter:function (value, row, index){
                if (value == 1) {
                    return "<span class='label label-success'>true</span>";
                } else {
                    return "<span class='label label-warning'>false</span>";
                }}
        },
    ]]
    , onClickRow: function (rowIndex, rowData) {
        SdcTreatPreCondition.qCRuleId = rowData.id;
    }
    , onLoadSuccess: function (rowData) {
    }
});


SdcTreatPreCondition.init = function (){
    var params = {};
    params.id = $("#id").val();
    var ajax = new $ax(Sys.backendPath + "/sdc/sdctreatplan/showDisplayRule", function (data) {
        $("#displayRule").val(data.showPremise);
        SdcTreatPreCondition.qCChange();
    },function (data){});
    ajax.setData(params);
    ajax.start();
};

SdcTreatPreCondition.qCChange = function () {
    var ajax = new $ax(Sys.backendPath + "/sdc/sdctreatplan/selectDisplayRule", function (data) {
        $('#showPremiseTable').datagrid({loadFilter: pagerFilter}).datagrid('loadData', data);
    }, function (data) {
        layer.msg("查询失败!" + data.responseJSON.message + "!", {icon: 2});
    });
    ajax.set({"id": $("#id").val()})
    ajax.set({"displayRule":encodeURIComponent(Common_GetValue('displayRule'))});
    ajax.set({"compare": SdcTreatPreCondition.qCRuleCompare});
    ajax.start();
}


function pagerFilter(data) {
    if (typeof data.length == 'number' && typeof data.splice == 'function') {// is array
        data = {
            total: data.length,
            rows: data
        }
    }
    var dg = $(this);
    var opts = dg.datagrid('options');
    var pager = dg.datagrid('getPager');
    pager.pagination({
        onSelectPage: function (pageNum, pageSize) {
            opts.pageNumber = pageNum;
            opts.pageSize = pageSize;
            pager.pagination('refresh', {pageNumber: pageNum, pageSize: pageSize});
            dg.datagrid('loadData', data);
        }
    });
    if (!data.originalRows) {
        data.originalRows = (data.rows);
    }
    var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
    var end = start + parseInt(opts.pageSize);
    data.rows = (data.originalRows.slice(start, end));
    return data;
}

SdcTreatPreCondition.compareAllRule = function () {
    SdcTreatPreCondition.qCRuleCompare = "all";
    SdcTreatPreCondition.qCChange();
}
SdcTreatPreCondition.compareYesRule = function () {
    SdcTreatPreCondition.qCRuleCompare = "yes";
    SdcTreatPreCondition.qCChange();
}

SdcTreatPreCondition.addSubmit = function (){
    var params = {};
    params.id = $("#id").val();
    params.displayRule = $("#displayRule").val();
    var ajax = new $ax(Sys.backendPath + "/sdc/sdctreatplan/saveDisplayRule", function (data) {
        //SdsDataConfig.initSourceForm();
        Sys.success("修改成功!");
        SdcTreatPreCondition.compareYesRule();
    }, function (data) {
        Sys.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(params)
    ajax.start();
};


SdcTreatPreCondition.addRule = function () {

    var href = Sys.backendPath + '/sds/sdsstandrulebase/to_add';
    var index = parent.layer.open({
        type: 2,
        title: ['<i class = "fa fa-object-group"></i>&nbsp;新增规则字典', 'padding-left:10px'],
        area: ['800px', '600px'], //宽高
        fix: false, //不固定
        skin: 'rbLBStyle  layer-ext-hisui',  //LB为layerBorder缩写
        //maxmin: true,
        content: href
    });
}


SdcTreatPreCondition.editRule = function () {
    if (SdcTreatPreCondition.qCRuleId == "" || SdcTreatPreCondition.qCRuleId == null) {
        Sys.info("请重新选择具体规则");
        return false;
    }

    var href = Sys.backendPath + '/sds/sdsstandrulebase/to_update/' + SdcTreatPreCondition.qCRuleId;
    var index = parent.layer.open({
        type: 2,
        title: ['<i class = "fa fa-object-group"></i>&nbsp;规则字典详情', 'padding-left:10px'],
        area: ['800px', '600px'], //宽高
        fix: false, //不固定
        skin: 'rbLBStyle  layer-ext-hisui',  //LB为layerBorder缩写
        content: href
    });
}


SdcTreatPreCondition.deleteRule = function () {
    if (SdcTreatPreCondition.qCRuleId == "" || SdcTreatPreCondition.qCRuleId == null) {
        Sys.info("请重新选择具体规则");
        return false;
    }
    var operation = function () {
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrulebase/delete", function (data) {
            Sys.success("删除成功!");
            SdcTreatPreCondition.qCChange();
        }, function (data) {
            Sys.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("SdsRuleBaseId", SdcTreatPreCondition.qCRuleId);
        ajax.start();
    };
    Sys.confirm("是否删除该规则字典?", operation);
}

$(function(){
    SdcTreatPreCondition.init();
});
